package com.mocha.bss.domain.rbac.config;

import cn.hutool.core.collection.CollectionUtil;
import com.mocha.bss.feign.bo.menu.FunctionBO;
import com.mocha.bss.feign.bo.menu.FunctionVO;
import com.mocha.bss.feign.bo.menu.MainMenuBO;
import com.mocha.bss.feign.bo.menu.SubMenuBO;
import com.pinghua.mocha.basics.MapKits;
import lombok.Getter;
import org.apache.commons.collections4.SetUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: songkui
 * @since: 2022/3/20 16:47
 */

public class MochaFunctionManager {
    private final static List<MainMenuBO> data = new ArrayList<>();

    @Getter
    private final static List<String> commonPattern = new ArrayList<>();

    private MochaFunctionManager(){}

    public static List<MainMenuBO> getAll(){
        return data;
    }

    public static void flushData(List<MainMenuBO> list, List<String> pattern){
        data.clear();
        data.addAll(list);

        commonPattern.clear();
        commonPattern.addAll(pattern);
    }

    public static List<FunctionBO> findByFunctionCode(List<String> codeList){
        if (CollectionUtil.isEmpty(codeList)){
            return null;
        }
        List<FunctionBO> funcList = new ArrayList<>(codeList.size());
        for (String code : codeList){
            FunctionBO functionBO = getFunctionByCode(code);
            if (null != functionBO){
                funcList.add(functionBO);
            }
        }
        return funcList;
    }

    private static FunctionBO getFunctionByCode(String code){
        for (MainMenuBO mainMenu : data){
            if (CollectionUtil.isNotEmpty(mainMenu.getSubMenuBOList())){
                List<SubMenuBO> subMenuBOList = mainMenu.getSubMenuBOList();
                for (SubMenuBO subMenuBO : subMenuBOList){
                    List<FunctionBO> functionList = subMenuBO.getFuncList();
                    if (CollectionUtil.isNotEmpty(functionList)){
                        for (FunctionBO functionBO : functionList){
                            if (code.equals(functionBO.getFuncCode())){
                                return functionBO;
                            }
                        }
                    }
                }
            }

        }
        return null;
    }

    public static Set<FunctionVO> findVOByCodes(Set<String> codeList){
        if (CollectionUtil.isEmpty(codeList)){
            return null;
        }

        Set<FunctionVO> funcSet = new HashSet<>(MapKits.mapSize(codeList.size()));
        for (String code : codeList){
            FunctionVO vo = getVOByCode(code);
            if (null != vo){
                funcSet.add(vo);
            }
        }
        return funcSet;
    }


    private static FunctionVO getVOByCode(String code){
        for (MainMenuBO mainMenu : data){
            if (code.equals(mainMenu.getCode())){
                return FunctionVO.mainToVo(mainMenu);
            }

            if (CollectionUtil.isNotEmpty(mainMenu.getSubMenuBOList())){
                List<SubMenuBO> subMenuBOList = mainMenu.getSubMenuBOList();
                for (SubMenuBO subMenuBO : subMenuBOList){
                    if (code.equals(subMenuBO.getCode())){
                        return FunctionVO.subToVo(subMenuBO);
                    }

                    List<FunctionBO> functionList = subMenuBO.getFuncList();
                    if (CollectionUtil.isNotEmpty(functionList)){
                        for (FunctionBO functionBO : functionList){
                            if (code.equals(functionBO.getFuncCode())){
                                return FunctionVO.boToVo(functionBO);
                            }
                        }
                    }
                }
            }

        }
        return null;
    }


}
